const { Controller } = require('egg')

class UpkeepController extends Controller {
  // 获得问题列表
  async getQuestionList() {
    const { ctx, app } = this
    ctx.validate({
      page: { type: 'int', required: true },
      size: { type: 'int', required: false, default: 5 }
    })
    const { page, size = 5 } = ctx.request.query
    const offset = (page - 1) * size

    let list = await app.model.Upkeep.findAll({
      offset,
      limit: size,
      order: [['id', 'DESC']]
    })

    list = JSON.parse(JSON.stringify(list))

    list.forEach((f) => {
      f.tag = f.tag.split(',')
      f.content = JSON.parse(f.content)
    })

    ctx.apiSuccess(list)
  }

  // 获得问题列表 admin
  async getQuestionListAdmin() {
    const { ctx, app } = this
    ctx.validate({
      page: { type: 'int', required: true },
      size: { type: 'int', required: false, default: 5 }
    })
    const { page, size = 5 } = ctx.request.query
    const offset = (page - 1) * size

    let list = await app.model.Upkeep.findAndCountAll({
      offset,
      limit: size,
      order: [['id', 'DESC']]
    })

    list = JSON.parse(JSON.stringify(list))

    list.rows.forEach((f) => {
      f.tag = f.tag.split(',')
      f.content = JSON.parse(f.content)
    })

    ctx.apiSuccess(list)
  }

  // 删除问题
  async deleteQuestion() {
    const { ctx, app } = this
    ctx.validate({
      id: { type: 'int', required: true }
    })
    const { id } = ctx.request.query

    const res = await app.model.Upkeep.destroy({
      where: {
        id
      }
    })
    if (!res) {
      return ctx.apiFail('删除失败')
    }
    ctx.apiSuccess('删除成功')
  }

  // 修改问题
  async updateQuestion() {
    const { ctx, app } = this
    ctx.validate({
      id: { type: 'int', required: true },
      title: { type: 'string', required: true },
      tag: { type: 'string', required: true },
      desc: { type: 'string', required: true },
      poster: { type: 'string', required: true },
      content: { type: 'string', required: true }
    })
    const { id, title, tag, desc, content, poster } = ctx.request.body
    const upkeep = await app.model.Upkeep.findByPk(id)
    if (!upkeep) {
      return ctx.apiFail('所修改的内容不存在')
    }
    upkeep.title = title
    upkeep.tag = tag
    upkeep.desc = desc
    upkeep.content = content
    upkeep.poster = poster
    const res = await upkeep.save()
    if (!res) {
      return ctx.apiFail('修改失败')
    }
    ctx.apiSuccess('修改成功')
  }

  // 添加问题
  async addQuestion() {
    const { ctx, app } = this
    ctx.validate({
      title: { type: 'string', required: true },
      tag: { type: 'string', required: true },
      desc: { type: 'string', required: true },
      poster: { type: 'string', required: true },
      content: { type: 'string', required: true }
    })
    const { title, tag, desc, content, poster } = ctx.request.body
    let upkeep = await app.model.Upkeep.create({
      title,
      tag,
      desc,
      content,
      poster
    })
    if (!upkeep) {
      return ctx.apiFail('添加失败')
    }
    upkeep = JSON.parse(JSON.stringify(upkeep))
    upkeep.tag = upkeep.tag.split(',')
    upkeep.content = JSON.parse(upkeep.content)
    ctx.apiSuccess(upkeep)
  }
}

module.exports = UpkeepController
